#!/bin/bash
# Date  : 2021-01-15 20:07:09
# Author: scg
# Email : uzz_scg@163.com
# Func  : 批量登陆Linux主机并执行命令
username="root"
password="bit123"
port="22"
timeout=3
#######################################################

login(){
    echo ""
    echo "-------------------------------------------------------- "
    echo "username: $username  password: $password  port: $port  timeout=$timeout"
    echo "command: $cmd"
    echo "--------------------------------------------------------"
    echo ""
    echo "[1. cd /root/deploy && /opt/kube/bin/kubectl delete -f resource-allocator.yaml]"
    pssh -l root -h ipMaster.txt 'cd /root/deploy && /opt/kube/bin/kubectl delete -f resource-allocator.yaml'
    echo "[2. cd /root/deploy && /opt/kube/bin/kubectl delete -f statusTracker.yaml]"
    pssh -l root -h ipMaster.txt 'cd /root/deploy && /opt/kube/bin/kubectl delete -f statusTracker.yaml'
    echo "[3. cd /root/deploy && /opt/kube/bin/kubectl delete -f storageClass-nfs.yaml]"
    pssh -l root -h ipMaster.txt 'cd /root/deploy && /opt/kube/bin/kubectl delete -f storageClass-nfs.yaml'
    echo "[4. cd /root/deploy && /opt/kube/bin/kubectl delete -f rabc-deploy.yaml]"
    pssh -l root -h ipMaster.txt 'cd /root/deploy && /opt/kube/bin/kubectl delete -f rabc-deploy.yaml'
    echo "[5. cd /root/deploy && /opt/kube/bin/kubectl delete -f pushgateway-conf.yml]"
    pssh -l root -h ipMaster.txt 'cd /root/deploy && /opt/kube/bin/kubectl delete -f pushgateway-conf.yml'
    echo "[6. cd /root/deploy && /opt/kube/bin/kubectl delete pod -A -l "app=scheduler" --force --grace-period=0]"
    pssh -l root -h ipMaster.txt 'cd /root/deploy && /opt/kube/bin/kubectl delete pod -A -l "app=scheduler" --force --grace-period=0'
    echo "[7. cd /root/deploy && /opt/kube/bin/kubectl delete pod -A -l "app=pushgateway" --force --grace-period=0]"
    pssh -l root -h ipMaster.txt 'cd /root/deploy && /opt/kube/bin/kubectl delete pod -A -l "app=pushgateway" --force --grace-period=0'
    echo "[8. cd /root/deploy && /opt/kube/bin/kubectl delete pod -A -l "app=task" --force --grace-period=0]"
    pssh -l root -h ipMaster.txt 'cd /root/deploy && /opt/kube/bin/kubectl delete pod -A -l "app=task" --force --grace-period=0'
    echo "[9. cd /root/deploy && /opt/kube/bin/kubectl delete pvc -A -l "pvc=nfs" --force --grace-period=0]"
    pssh -l root -h ipMaster.txt 'cd /root/deploy && /opt/kube/bin/kubectl delete pvc -A -l "pvc=nfs" --force --grace-period=0'
    echo "[10 ./opt/kube/bin/etcdctl del /registry/persistentvolumeclaims/scheduler-ns/scheduler-ns-pvc]"
    pssh -l root -h ipMaster.txt '/opt/kube/bin/etcdctl del /registry/persistentvolumeclaims/scheduler-ns/scheduler-ns-pvc'
    echo "[11. cd /root/deploy && /opt/kube/bin/kubectl delete pv --all]"
    pssh -l root -h ipMaster.txt 'cd /root/deploy && /opt/kube/bin/kubectl delete pv --all'
    echo "[12.cd /root/deploy/delNsPv && chmod a+x delSaPvcPvNs.sh && ./delSaPvcPvNs.sh]"
    pssh -l root -h ipMaster.txt 'cd /root/deploy/delNsPv && chmod a+x delSaPvcPvNs.sh && ./delSaPvcPvNs.sh'
    echo "[13. cd /root/deploy && /opt/kube/bin/kubectl delete ns -A -l "namespace=scheduler"]"
    pssh -l root -h ipMaster.txt 'cd /root/deploy && /opt/kube/bin/kubectl delete ns -A -l "namespace=scheduler"'
    echo "[14./opt/kube/bin/etcdctl del /registry/namespaces/scheduler-ns]"
    pssh -l root -h ipMaster.txt '/opt/kube/bin/etcdctl del /registry/namespaces/scheduler-ns'
    echo "[15.cd /root/deploy && /opt/kube/bin/kubectl delete ns -A -l "namespace=task"]"
    pssh -l root -h ipMaster.txt 'cd /root/deploy && /opt/kube/bin/kubectl delete ns -A -l "namespace=task"'
    echo "[16.cd /root/deploy && chmod a+x clearEtcdData && ./clearEtcdData && rm -rf /nfsdata/*]"
    pssh -l root -h ipMaster.txt 'cd /root/deploy && chmod a+x clearEtcdData && ./clearEtcdData && rm -rf /nfsdata/*'
    echo "[17.cd /root && rm -rf deploy && mkdir deploy]"
    pssh -l root -h ipMaster.txt 'cd /root && rm -rf deploy && mkdir deploy'
    echo "[18.pscp -l root -h ipMaster.txt -r /root/deploy-work/master_yaml/deploy1/* /root/deploy]"
    pscp -l root -h ipMaster.txt -r /root/deploy-work/master_yaml/deploy1/* /root/deploy
    echo "[19.cd /root/deploy && chmod a+x edit_yaml_ip_scg.sh && ./edit_yaml_ip_scg.sh && rm -rf /var/log/*] "
    pssh -l root -h ipMaster.txt 'cd /root/deploy && chmod a+x edit_yaml_ip_scg.sh && ./edit_yaml_ip_scg.sh'
    i=0
    for host in `cat ipMaster.txt`
    do
        i=$(($i+1))
        cmd0="cd /root/deploy && sed -i "s/#.#.#.#/$i/g" resource-allocator.yaml && sed -i "s/#.#.#.#/$i/g" statusTracker.yaml"
        result=""
        result=`sshpass -p "$password" ssh -p $port -o StrictHostKeyChecking=no -o ConnectTimeout=$timeout $username@$host $cmd0 `
        echo $i $host
        echo $result >> result.txt
    done

}

login
ls
